#include "sys.h"
#include "gd32f10x.h"
static volatile uint32_t uwTick;
/*******************************************************************************************
  * @Func		systick_config(void)
  * @Brief    	滴答时钟配置
  * @Param		None
  * @Retval		None
  * @Date     	2018/9/16
 *******************************************************************************************/
void systick_config(void)
{
	/* setup systick timer for 1Mhz interrupts */
	if (SysTick_Config(SystemCoreClock / 1000000U))	while (1);	//1Mhz
//	if (SysTick_Config(SystemCoreClock / 1000U))	while (1);	//1khz
//	if (SysTick_Config(SystemCoreClock / 1U))		while (1);	//1 hz
    /* configure the systick handler priority */
    NVIC_SetPriority(SysTick_IRQn, 0x00U);
}
/*******************************************************************************************
  * @Func		GD_decTick(void)
  * @Brief    	延时缓存时间周期减
  * @Param		None
  * @Retval		None
  * @Date     	2018/9/16
 *******************************************************************************************/
void GD_decTick(void)
{
	if(0U != uwTick){
	uwTick--;
	}
}
/*******************************************************************************************
  * @Func		delay_us(uint32_t nTime)
  * @Brief    	微秒延时
  * @Param		nTime ：延时时间
  * @Retval		None
  * @Date     	2018/9/16
 *******************************************************************************************/
void delay_us(uint32_t nTime)
{
	uwTick = nTime;
	while(0U != uwTick){
    }
}
/*******************************************************************************************
  * @Func		delay_us(uint32_t nTime)
  * @Brief    	毫秒延时
  * @Param		nTime ：延时时间
  * @Retval		None
  * @Date     	2018/9/16
 *******************************************************************************************/
void delay_ms(uint32_t nTime)
{
	uwTick = nTime*1000;
	while(0U != uwTick){
    }
}

__weak void SysTick_Callback(void)
{
  /* NOTE : This function Should not be modified, when the callback is needed,
            the SysTick_Callback could be implemented in the user file
   */
}
/*******************************************************************************************
  * @Func		SysTick_Handler(void)
  * @Brief    	滴答时钟中断函数
  * @Param		None
  * @Retval		None
  * @Date     	2018/9/16
 *******************************************************************************************/

void SysTick_Handler(void)  //1Mhz
{
	static uint32_t time =0;  
	time++;
	GD_decTick();
	if(time == LOOP_TIME){		//1M/LOOP_TIME=1M/10000=100hz
		SysTick_Callback();
    time = 0;
  }
}
